<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="数据库-分页" tabindex="-1"><a class="header-anchor" href="#数据库-分页"><span>数据库：分页</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#basic-usage">基本用法</a>
<ul>
<li><a href="#paginating-query-builder-results">查询构造器分页</a></li>
<li><a href="#paginating-eloquent-results">Eloquent 分页</a></li>
<li><a href="#manually-creating-a-paginator">手动创建分页</a></li>
</ul>
</li>
<li><a href="#displaying-pagination-results">显示分页结果</a>
<ul>
<li><a href="#converting-results-to-json">将结果转换为 JSON</a></li>
</ul>
</li>
<li><a href="#customizing-the-pagination-view">自定义分页视图</a></li>
<li><a href="#paginator-instance-methods">分页器实例方法</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>在其他框架中，分页可能是非常痛苦的。Laravel的分页器将 <a href="https://learnku.com/docs/laravel/6.x/queries" target="_blank" rel="noopener noreferrer">查询构造器</a> 和 <a href="https://learnku.com/docs/laravel/6.x/eloquent" target="_blank" rel="noopener noreferrer">Eloquent ORM</a> 结合起来，提供了方便、易用的数据库结果集分页。通过分页器生成的 HTML 兼容 <a href="https://getbootstrap.com/" target="_blank" rel="noopener noreferrer">Bootstrap CSS 框架</a>。</p>
<h2 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h2>
<h3 id="查询构造器分页" tabindex="-1"><a class="header-anchor" href="#查询构造器分页"><span>查询构造器分页</span></a></h3>
<p>数据分页有多种方法。最简单的是使用 <a href="https://learnku.com/docs/laravel/6.x/queries" target="_blank" rel="noopener noreferrer">查询构造器</a> 或 <a href="https://learnku.com/docs/laravel/6.x/eloquent" target="_blank" rel="noopener noreferrer">Eloquent query</a> 的 <code v-pre>paginate</code> 方法。<code v-pre>paginate</code> 方法根据用户浏览的当前页码，自动设置恰当的偏移量 offset 和限制数 limit。默认情况下，HTTP 请求中的 <code v-pre>page</code> 查询参数值被当作当前页的页码。 Laravel 会自动检测该值，并自动将其插入到分页器生成的链接中。</p>
<p>在下面的例子中，传递给 <code v-pre>paginate</code> 方法的唯一参数是你想要在每页显示的记录数。在此例中，我们指定要在每页显示 <code v-pre>15</code> 条数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 显示应用程序中的所有用户.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.index'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'users'</span> <span class="token operator">=></span> <span class="token variable">$users</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：目前，Laravel无法高效地执行使用 <code v-pre>groupBy</code> 语句的分页操作。如果你需要对使用了 <code v-pre>groupBy</code> 的结果集分页，建议你手动查询数据库并创建分页。</p>
</blockquote>
<h4 id="简单分页" tabindex="-1"><a class="header-anchor" href="#简单分页"><span>简单分页</span></a></h4>
<p>如果你只需要在分页视图中简单地显示「下一页」和「上一页」的链接，你可以使用 <code v-pre>simplePaginate</code> 方法来执行更高效地查询。这在数据量很大且不需要在渲染视图时显示每页的页码时非常有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">simplePaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="eloquent-分页" tabindex="-1"><a class="header-anchor" href="#eloquent-分页"><span>Eloquent 分页</span></a></h3>
<p>你还可以对 <a href="https://learnku.com/docs/laravel/6.x/eloquent" target="_blank" rel="noopener noreferrer">Eloquent</a> 查询结果进行分页。在下面的例子中，我们将 <code v-pre>User</code> 模型按每页 <code v-pre>15</code> 条分页。如你所见，其语法与查询构造器分页基本相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你还可以在调用 <code v-pre>paginate</code> 之前，在查询中设置诸如 <code v-pre>where</code> 从句的其它约束：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你还可以在 Eloquent 分页中使用 <code v-pre>simplePaginate</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">simplePaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="手动创建分页" tabindex="-1"><a class="header-anchor" href="#手动创建分页"><span>手动创建分页</span></a></h3>
<p>有时你可能希望手动创建分页，并传递一个数组集给它。可以通过创建 <code v-pre>Illuminate\Pagination\Paginator</code> 或 <code v-pre>Illuminate\Pagination\LengthAwarePaginator</code> 实例来实现，这取决于你的需要。</p>
<p><code v-pre>Paginator</code> 类不需要知道结果集的总数；然而，这样一来，这个类就没办法获知最后一页的索引。<code v-pre>LengthAwarePaginator</code> 接受和 <code v-pre>Paginator</code> 几乎相同的参数；不过，它会计算结果集的总数。</p>
<p>换句话说，<code v-pre>Paginator</code> 相当于查询构造器或 Eloquent 的 <code v-pre>simplePaginate</code> 方法，而 <code v-pre>LengthAwarePaginator</code> 相当于 <code v-pre>paginate</code> 方法。</p>
<blockquote>
<p>{注意} 在手动创建分页实例时，需要人为「切割」传递给分页实例的结果数组。如果你对此没有把握，请参考 PHP 的 <a href="https://secure.php.net/manual/en/function.array-slice.php" target="_blank" rel="noopener noreferrer">array_slice</a> 方法。</p>
</blockquote>
<h2 id="显示结果集" tabindex="-1"><a class="header-anchor" href="#显示结果集"><span>显示结果集</span></a></h2>
<p>调用 <code v-pre>paginate</code> 方法时，你会得到 <code v-pre>Illuminate\Pagination\LengthAwarePaginator</code> 实例，调用 <code v-pre>simplePaginate</code> 方法时，你会得到 <code v-pre>Illuminate\Pagination\Paginator</code> 实例。这些对象提供了几个描述结果集的方法。除了这些辅助方法，分页器的实例是迭代器，可以像数组一样循环。所以，当你得到结果后，你可以使用 <a href="https://learnku.com/docs/laravel/6.x/blade" target="_blank" rel="noopener noreferrer">Blade</a> 模版来显示数据、渲染分页链接：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"container"</span><span class="token operator">></span></span>
<span class="line">    @<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">    @<span class="token keyword">endforeach</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>links</code> 方法会渲染结果集中剩余页面的链接。每个链接都已经包含了 <code v-pre>page</code> URL 变量。记住， <code v-pre>links</code> 方法生成的 HTML 兼容 <a href="https://getbootstrap.com/" target="_blank" rel="noopener noreferrer">Bootstrap CSS 框架</a>。</p>
<h4 id="自定义分页器-uri" tabindex="-1"><a class="header-anchor" href="#自定义分页器-uri"><span>自定义分页器 URI</span></a></h4>
<p><code v-pre>withPath</code> 方法允许你在生成分页链接时自定义URI。例如，如果你想生成像 <code v-pre>http://example.com/custom/url?page=N</code> 的分页链接，你应该传递 <code v-pre>custom/url</code> 参数给 <code v-pre>withPath</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">withPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'custom/url'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="附加参数到分页链接" tabindex="-1"><a class="header-anchor" href="#附加参数到分页链接"><span>附加参数到分页链接</span></a></h4>
<p>你可以使用 <code v-pre>appends</code> 方法向分页链接中添加查询参数。例如，要在每个分页链接中添加 <code v-pre>sort=votes</code> ，你应该这样调用 <code v-pre>appends</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">appends</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sort'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'votes'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你希望向分页器的 URL 添加「哈希片段」，你可以使用 <code v-pre>fragment</code> 方法。要在每页链接中添加 <code v-pre>#foo</code> ，应该这样调用 <code v-pre>fragment</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">fragment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="调整分页链接窗口" tabindex="-1"><a class="header-anchor" href="#调整分页链接窗口"><span>调整分页链接窗口</span></a></h4>
<p>你可以控制在分页器 URL「窗口」的每一侧显示多少个附加链接。默认情况下，主分页链接的每侧显示三个链接。但是，你可以使用 <code v-pre>onEachSide</code> 方法来控制这个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">onEachSide</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="将结果转换为-json" tabindex="-1"><a class="header-anchor" href="#将结果转换为-json"><span>将结果转换为 JSON</span></a></h3>
<p>Laravel 分页器类实现了 <code v-pre>Illuminate\Contracts\Support\Jsonable</code> 接口契约，提供了 <code v-pre>toJson</code> 方法，所以可以方便的将分页结果转换为 JSON 。你也可以通过来自路由或控制器的操作返回分页实例来将其转换为 JSON：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>来自分页器的 JSON 包括诸如 <code v-pre>total</code>，<code v-pre>current_page</code>，<code v-pre>last_page</code> 等元数据信息。实际结果对象将通过 JSON 数组的 <code v-pre>data</code> 键提供。以下是通过自路由中分页器实例的方式创建 JSON 的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">   <span class="token string double-quoted-string">"total"</span><span class="token punctuation">:</span> <span class="token number">50</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"per_page"</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"current_page"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"last_page"</span><span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"first_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=1"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"last_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=4"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"next_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=2"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"prev_page_url"</span><span class="token punctuation">:</span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"path"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"from"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"to"</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"data"</span><span class="token punctuation">:</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 结果对象集</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 结果对象集</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">   <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义分页视图" tabindex="-1"><a class="header-anchor" href="#自定义分页视图"><span>自定义分页视图</span></a></h2>
<p>默认情况下，渲染视图来显示分页链接是与 Bootstrap CSS 框架兼容的。然而，如果你不想使用 Bootstrap，你可以自由的选择你自己定制的视图来渲染这些链接。在调用分页器实例的 <code v-pre>links</code> 方法时，将视图名称作为传递给它的第一个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$paginator</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 传递数据给视图...</span></span>
<span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$paginator</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>但是，自定义分页视图最简单的方法是使用 <code v-pre>vendor:publish</code> 命令将它们输出到<code v-pre>resources/views/vendor</code> 目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>laravel<span class="token operator">-</span>pagination</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令将在 <code v-pre>resources/views/vendor/pagination</code> 目录中放置这些视图。内置在该目录下的 <code v-pre>bootstrap-4.blade.php</code> 文件提供了默认的分页视图。你可以编辑这个文件来修改分页的 HTML。</p>
<p>如果你想要定义不同的文件作为默认的分页视图，你需要在 <code v-pre>AppServiceProvider</code> 中使用分页器的 <code v-pre>defaultView</code> 和 <code v-pre>defaultSimpleView</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Pagination<span class="token punctuation">\</span>Paginator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">defaultView</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">defaultSimpleView</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="分页器实例方法" tabindex="-1"><a class="header-anchor" href="#分页器实例方法"><span>分页器实例方法</span></a></h2>
<p>每个分页器实例提供了如下方法来获取附加的分页信息：</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$results-&gt;count()</code></td>
<td>获取当前页数据的数量。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;currentPage()</code></td>
<td>获取当前页页码。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;firstItem()</code></td>
<td>获取结果集中第一条数据的结果编号。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;getOptions()</code></td>
<td>获取分页器选项。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;getUrlRange($start, $end)</code></td>
<td>创建分页 URL 的范围。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;hasMorePages()</code></td>
<td>是否有多页。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;items()</code></td>
<td>获取当前页的所有项。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;lastItem()</code></td>
<td>获取结果集中最后一条数据的结果编号。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;lastPage()</code></td>
<td>获取最后一页的页码。(在 <code v-pre>simplePaginate</code> 无效)。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;nextPageUrl()</code></td>
<td>获取下一页的 URL。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;onFirstPage()</code></td>
<td>当前页是否为第一页。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;perPage()</code></td>
<td>每页的数据条数。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;previousPageUrl()</code></td>
<td>获取前一页的 URL。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;total()</code></td>
<td>数据总数（在 <code v-pre>simplePaginate</code> 无效）。</td>
</tr>
<tr>
<td><code v-pre>$results-&gt;url($page)</code></td>
<td>获取指定页的 URL。</td>
</tr>
</tbody>
</table>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/pagination/5172" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/pag...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/pagination/5172" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/pag...</a></p>
</blockquote>
</div></template>


